<template>
  <div>
    <el-button type="text" @click="centerDialogVisible = true">点击打开</el-button>

    <el-dialog
      title="用户注册"
      :visible.sync="centerDialogVisible"
      width="30%"
      center>
          <span>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
              <el-form-item label="用户名" prop="name">
                <el-input v-model="ruleForm.username"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="pass">
                <el-input type="password" v-model="ruleForm.userpassword" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="确认密码" prop="checkPass">
                <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item prop="email" label="邮箱">
                <el-input v-model="ruleForm.email"></el-input>
              </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="sendMail()">获取验证码</el-button>
            </el-form-item>
            <el-form-item label="验证码" prop="code">
              <el-input v-model="ruleForm.code"></el-input>
            </el-form-item>
            </el-form>
          </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">重 置</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">注 册</el-button>
      </span>
    </el-dialog>

  </div>

</template>

<script>
  import axios from 'axios';

  export default {
    data() {
      var validateEmail = (rule, value, callback) => {
        const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/
        if (!value) {
          return callback(new Error('邮箱不能为空'));
        }
        if (!mailReg.test(value)) {
          return callback(new Error('请输入正确的邮箱格式'));
        }
        setTimeout(() => {
          // axios.post("/api/findByEmail?email="+this.ruleForm2.email).then(res=>{
          //   console.log(res.data.success)
          //   if (res.data=="0"){
          //     callback(new Error('邮箱已被注册！'));
          //   }else{
          //     callback();
          //   }
          // })
        }, 1000);
      };
      var validateloginName = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('登录名不能为空'));
        }
        setTimeout(() => {
          axios.post("",this.ruleForm2).then(res=>{
            if (!res.data.success){
              callback(new Error('用户名重复'));
            }else{
              callback();
            }
          })
        }, 1000);
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.pass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      var validatecode = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入验证码'));
        }
      };
      return {
        centerDialogVisible: false,
        ruleForm: {
          username: '',
          userpassword: '',
          checkPass: '',
          email: '',
          code: ''
        },
        rules: {
          email: [
            { validator: validateEmail, trigger: 'blur' }
          ],
          loginName:[
            { validator: validateloginName, trigger: 'blur' }
          ],
          password: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkpassword: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          codes: [
            { validator: validatecode, trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      submitForm(formName) {
        axios.post("http://127.0.0.1:8200/fancy-music-user/user/registry", this.ruleForm).then(res => {
          if (res.data == 1) {
            alert("注册成功")
          }
          if (res.data == 2) {
            alert("验证码错误")
          }
        })
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      sendMail: function () {
        axios.get("http://127.0.0.1:8200/fancy-music-user/user/sendMail?mail=" + this.ruleForm.email).then(res => {
          if (res.data == 1) {
            alert("发送成功")
          }
        })
      }
    }
  }

</script>

<style scoped>

</style>
